<?php

namespace application\Table;

use library\Core\Db\CDbTable;
use library\Core\Db\CDbCriteria;

class Base extends CDbTable {
    
    public $_DbName_ = '';
    public $_TableName_ = '';
    public $_SeleteAdapter = 'slave';
    public $_ModifyAdapter = 'master';
    
    private $_calledClass = NULL;
    
    public function __construct() {
       
    }
    
    public static function getInstance(){
        $calledClass = get_called_class();
        return new $calledClass();
    }
    
    public function insert($data){
        
        $calledClass = get_called_class();
        
        $criteria = new CDbCriteria();
        $criteria->insert($data);
        
        $criteriaBuilder = $criteria->getCriteriaBuilder(new $calledClass());
        
        $operator = new Core_Db_CDbOperator(); 
        return $operator->insert($criteriaBuilder, $this->_ModifyAdapter);
    }
    
    public function update(Core_Db_CDbCriteria $criteria){
    
        $calledClass = get_called_class();
    
        $criteriaBuilder = $criteria->getCriteriaBuilder(new $calledClass());
    
        $operator = new Core_Db_CDbOperator();
        return $operator->update($criteriaBuilder, $this->_ModifyAdapter);
    }
    
    public function delete(Core_Db_CDbCriteria $criteria){
    
        $calledClass = get_called_class();
    
        $criteriaBuilder = $criteria->getCriteriaBuilder(new $calledClass());
    
        $operator = new Core_Db_CDbOperator();
        return $operator->delete($criteriaBuilder, $this->_ModifyAdapter);
    }
    
    public function query(Core_Db_CDbCriteria $criteria, $isArray = false){
        
        $calledClass = get_called_class();
        
        $criteria = $criteria->getCriteriaBuilder(new $calledClass());
        
        $operator = new Core_Db_CDbOperator();
        
        return $operator->query($criteria,$isArray);
    
    }
    
    public function queryRow(Core_Db_CDbCriteria $criteria, $isArray = false){
    
        $calledClass = get_called_class();
    
        $criteria = $criteria->getCriteriaBuilder(new $calledClass());
    
        $operator = new Core_Db_CDbOperator();
    
        return $operator->queryRow($criteria,$isArray);
    
    }
    
    public function queryOne(Core_Db_CDbCriteria $criteria, $isArray = false){
    
        $calledClass = get_called_class();
    
        $criteria = $criteria->getCriteriaBuilder(new $calledClass());
    
        $operator = new Core_Db_CDbOperator();
    
        return $operator->queryOne($criteria,$isArray);
    
    }
    
    
}
